package com.janal.model.Postbar.bar;

import com.janal.model.Basedao.Basedao;
import com.janal.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class PostBarDaoImpl implements PostBarDao{

    @Override
    public boolean createPostBarDao(Connection conn, Post_bar pb) throws Exception {
        PreparedStatement pstm = null;
        if(conn != null) {
            String sql = "insert into post_bar(bar_id,bar_name,bar_host," +
                    "notice,status) values(?,?,?,?,?)";
            Object[] params = {pb.getBar_id(), pb.getBar_name(), pb.getBar_host(), pb.getNotice(),0};
            int updateRows = Basedao.execute(conn,sql,params,pstm);
            JdbcUtils.close(pstm,null);
            if(updateRows > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Post_bar> getAllBarMessageDao(Connection conn, int status) throws Exception {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Post_bar> lpb = null;
        if(conn != null){
            String sql = "select * from post_bar where status = ?";
            Object[] params = {status};
            rs = Basedao.execute(conn,sql,params,rs,pstm);
            lpb = new ArrayList<>();
            while(rs.next()){
                Post_bar pb = new Post_bar();
                pb.setBar_id(rs.getString("bar_id"));
                pb.setBar_name(rs.getString("bar_name"));
                pb.setBar_host(rs.getString("bar_host"));
                pb.setNotice(rs.getString("notice"));
                lpb.add(pb);
            }
            JdbcUtils.close(rs,pstm,null);
        }
        return lpb;
    }
}
